<template>
	<view>
		<scroll-view scroll-y class="page">
			<view class="padding text-center title">发布商品</view>
			<form>
				<view class="cu-form-group">
					<view class="title">商品名称</view>
					<input placeholder="请输入商品名称" name="input" v-model="prod.name"></input>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">所属分类</view>
					<picker @change="PickerChange" :value="index" :range="picker">
						<view class="picker">
							{{index>-1?picker[index]:'请选择所属分类'}}
						</view>
					</picker>
				</view>
				<view class="cu-bar bg-white margin-top">
					<view class="action">
						商品展示
					</view>
					<view class="action">
						{{imgList.length}}/4
					</view>
				</view>
				<view class="cu-form-group">
					<view class="grid col-4 grid-square flex-sub">
						<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
						 <image :src="imgList[index]" mode="aspectFill"></image>
							<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
								<text class='cuIcon-close'></text>
							</view>
						</view>
						<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
							<text class='cuIcon-cameraadd'></text>
						</view>
					</view>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">商品原价</view>
					<input placeholder="请输入商品原价" name="input" v-model="prod.price"></input>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">商品优惠价</view>
					<input placeholder="请输入商品优惠价" name="input" v-model="prod.promotionPrice"></input>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">商品描述</view>
					<textarea maxlength="-1" :disabled="modalName!=null" @input="textareaAInput" placeholder="清输入商品描述" v-model="prod.description"></textarea>
				</view>
			</form>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-red margin-tb-sm lg" @click="publish">发布</button>
			</view>
			<view class="cu-tabbar-height"></view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: -1,
				picker: [],
				imgList: [],
				modalName: null,
				prod: {},
				cates: []
			}
		},
		onLoad() {
			this.$apis.getProdCategoryList().then((res) => {
				this.cates = res;
				res.forEach((ele, ind) => {
					this.picker.push(ele.name)
				})
			})
		},
		methods: {
			PickerChange(e) {
				this.index = e.detail.value
			},
			publish(){
				this.prod.pic =  this.imgList.join(",");
				this.cates.forEach(ele => {
					if(ele.name == this.picker[this.index]){
						this.prod.productCategoryId = ele.id;
					}
				})
				this.$apis.saveProduct(this.prod).then((res) => {
					uni.navigateTo({
						url: '/pages/store/index?id=' + this.storeId
					})
				})
			},
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			textareaAInput(e) {
				this.textareaAValue = e.detail.value
			},
			textareaBInput(e) {
				this.textareaBValue = e.detail.value
			}
		}
	}
</script>

<style>

</style>
